package com.huan.sockword;

import android.app.KeyguardManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.assetsbasedata.AssetsDatabaseManager;
import com.huan.greendao.entity.greendao.CET4Entity;
import com.huan.greendao.entity.greendao.CET4EntityDao;
import com.huan.greendao.entity.greendao.DaoMaster;
import com.huan.greendao.entity.greendao.DaoSession;
import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechListener;
import com.iflytek.cloud.speech.SpeechSynthesizer;
import com.iflytek.cloud.speech.SpeechUser;
import com.iflytek.cloud.speech.SynthesizerListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, SynthesizerListener {

    //用来显示单词和音标

    private TextView timeText,dateText,wordText,englishText;
    private ImageView playVioce;
    private String mMonth,mDay,mWay,mHours,mMinute;

    private SpeechSynthesizer speechSynthesizer; //合成对象

    //锁屏
    private KeyguardManager km;
    private KeyguardManager.KeyguardLock k1;
    private RadioGroup radioGroup;
    private RadioButton radioOne,radioTwo,radioThree;

    private SharedPreferences sharedPreferences;

    SharedPreferences.Editor editor=null;

    int j=0;

    List<Integer> list;
    List<CET4Entity> datas;
    int k;



/*
* * 手指按下时位置坐标为（x1,y1）
 * 手指离开时位置坐标为 (x2,y2)
* */

    float x1=0;
    float y1=0;
    float x2=0;
    float y2=0;


    private SQLiteDatabase db;
    private DaoMaster mDaoMaster,dbMaster;
    private DaoSession mDaoSession,dbSession;

    private CET4EntityDao questionDao,dbDao;
    




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //将锁屏页面显示到手机屏幕的最上层

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

        setContentView(R.layout.activity_main);
        init();
    }



    /*
    * 初始化控件
    * */

    public void init(){
        //初始化轻量级数据库
        sharedPreferences=getSharedPreferences("share", Context.MODE_PRIVATE);

        editor=sharedPreferences.edit();
        list=new ArrayList<Integer>();

    //添加是个20以内的随机数


        Random r=new Random();
    int i;
    while (list.size()<10){
        i=r.nextInt(20);
        if(!list.contains(i)){
            list.add(i);
        }
    }

    /*
    * 得到键盘锁管理对象
    * */

    km=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
    k1=km.newKeyguardLock("unLock");

    //初始化，只需要调用一次

        AssetsDatabaseManager.initManager(this);

        AssetsDatabaseManager mg=AssetsDatabaseManager.getManager();

        SQLiteDatabase db1=mg.getDatabase("word.db");

        mDaoMaster=new DaoMaster(db1);

        mDaoSession=mDaoMaster.newSession();

        questionDao=mDaoSession.getCET4EntityDao();

        DaoMaster.DevOpenHelper helper=new DaoMaster.DevOpenHelper(this,"word.db",null );


        /*
        * 初始化数据库
        * */


        db=helper.getWritableDatabase();
        dbMaster=new DaoMaster(db);
        dbSession=dbMaster.newSession();
        dbDao=dbSession.getCET4EntityDao();


        /*
        * 控件初始化
        * */


        timeText=(TextView)findViewById(R.id.time_text);

        dateText=(TextView)findViewById(R.id.date_text);

        wordText=(TextView)findViewById(R.id.word_text);

        englishText=(TextView)findViewById(R.id.english_text);

        playVioce=(ImageView)findViewById(R.id.play_voice);

        playVioce.setOnClickListener(this);

        radioGroup=(RadioGroup)findViewById(R.id.choose_group);

        radioOne=(RadioButton)findViewById(R.id.choose_btn_one);

        radioTwo=(RadioButton)findViewById(R.id.choose_btn_two);

        radioThree=(RadioButton)findViewById(R.id.choose_btn_three);

        radioGroup.setOnCheckedChangeListener(this);

        setParam();
        SpeechUser.getUser().login(MainActivity.this,null,null,"addip=573a7bf0",listener);




    }


    @Override
    protected void onStart() {
        super.onStart();


        /*
        * 获取系统时间，并设置将其显示出来
        * */

        Calendar calendar=Calendar.getInstance();
        mMonth=String.valueOf(calendar.get(Calendar.MONTH)+1);
        mDay=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        mWay=String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));

        /*
        * 如果小时是个数
        * 则在前面加一个0
        * */

        if(calendar.get(Calendar.HOUR)<10){
            mHours="0"+calendar.get(Calendar.HOUR);
        }else {
            mHours=String.valueOf(calendar.get(Calendar.HOUR));
        }
        /*
         * 如果分钟是个数
         * 则在前面加一个0
         * */
        if(calendar.get(Calendar.MINUTE)<10){
            mMinute="0"+calendar.get(Calendar.HOUR);
        }else {
            mMinute=String.valueOf(calendar.get(Calendar.HOUR));
        }

        /*
        * 获取星期
        * 并设置出来
        * */

        if("1".equals(mWay)){
            mWay="天";

        }else if ("2".equals(mWay)){
            mWay="一";
        }else if ("3".equals(mWay)){
            mWay="二";
        }else if ("4".equals(mWay)){
            mWay="三";
        }else if ("5".equals(mWay)){
            mWay="四";
        }else if ("6".equals(mWay)){
            mWay="五";
        }else if ("7".equals(mWay)){
            mWay="六";
        }

        timeText.setText(mHours+":"+mMinute);
        dateText.setText(mHours+"月"+mDay+"日"+"  "+"星期"+mWay);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.play_voice:
                String text=wordText.getText().toString();
                speechSynthesizer.startSpeaking(text,this);
                break;
        }
    }

    @Override
    public void onSpeakBegin() {

    }

    @Override
    public void onBufferProgress(int i, int i1, int i2, String s) {

    }

    @Override
    public void onSpeakPaused() {

    }

    @Override
    public void onSpeakResumed() {

    }

    @Override
    public void onSpeakProgress(int i, int i1, int i2) {

    }

    @Override
    public void onCompleted(SpeechError speechError) {

    }

//    通用回调接口

    private SpeechListener listener=new SpeechListener() {
        //消息回调
        @Override
        public void onEvent(int i, Bundle bundle) {

        }

        @Override
        public void onData(byte[] bytes) {

        }

        @Override
        public void onCompleted(SpeechError speechError) {

        }
    };


    /*
    * 初始化语音播报
    * */

    public void setParam(){
        speechSynthesizer=SpeechSynthesizer.createSynthesizer(this);
        speechSynthesizer.setParameter(SpeechConstant.VOICE_NAME,"xiaoyan");
        speechSynthesizer.setParameter(SpeechConstant.SPEED,"50");
        speechSynthesizer.setParameter(SpeechConstant.VOLUME,"50");
        speechSynthesizer.setParameter(SpeechConstant.PITCH,"50");


    }
}
